package com.example.redis.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.redis.dao.RedisLoginDao;
import com.example.redis.service.RedisLoginService;

@Service
public class RedisLoginServiceImpl implements RedisLoginService {

	@Autowired
	private RedisLoginDao redisLoginDao;

	@Override
	public void login(String username, String token) {
		synchronized (this) {
			redisLoginDao.setLoginToken(username, token);
		}
	}

	@Override
	public void logout(String username) {
		synchronized (this) {
			redisLoginDao.deleteLoginToken(username);
		}
	}

	@Override
	public String getLoginToken(String username) {
		synchronized (this) {
			return redisLoginDao.getLoginToken(username);
		}
	}

	@Override
	public Boolean validate(String username, String token) {
		synchronized (this) {
			return token.equals(redisLoginDao.getLoginToken(username));
		}
	}

}
